package com.zryc.shop.ums.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.ums.entity.Resource;
import com.zryc.shop.ums.service.IResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@RestController
@Api(tags = "资源管理接口列表")
@RequestMapping("/ums/resource")
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @GetMapping("/list")
    @ApiOperation("分页查询全部资源列表")
    public Result list(@RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "10") @ApiParam(value = "每页大小", required = true) Integer pageSize,
                       @RequestParam(required = false) @ApiParam(value = "资源名称") String nameKeyword,
                       @RequestParam(required = false) @ApiParam(value = "资源路径") String urlKeyword,
                       @RequestParam(required = false) @ApiParam(value = "资源分类") Integer categoryId
    ) {
        return Result.ok(resourceService.list(pageNum, pageSize,nameKeyword,urlKeyword,categoryId));
    }

    @GetMapping("/listAll")
    @ApiOperation("分页查询全部资源列表")
    public Result listAll() {
        return Result.ok(resourceService.list());
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除资源")
    public Result delete(@PathVariable @ApiParam(value = "资源id", required = true) Long id) {
        return Result.ok(resourceService.deleteResourceById(id));
    }

    @PostMapping("/create")
    @ApiOperation("新增资源")
    public Result create(@RequestBody Resource resource) {
        return Result.ok(resourceService.save(resource));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新资源信息")
    public Result update(@PathVariable @ApiParam(value = "资源id", required = true) Long id, @RequestBody Resource resource) {
        return Result.ok(resourceService.updateResource(id, resource));
    }


}